//页面数据：运费模板模块
import {
	getToken,
	setToken
} from '@/utils/token.js'
import {
	addFriends,
	updAddFriends,
	usrBlackListInsert,
	usrBlackListRemove,
	queryBlackList,
	queryApplyFriend,
	deleteFriend,
	getUnReadListByEntity
} from '@/common/api/msg.js'
import store from '@/store/index.js'
const state = {
	chatList: [], //联系人列表
	chatItem: getToken('chatItem') || {},
	useState: {
		isMyFriend: false, //是我的好友
		isObjFriend: false, //是对方的好友
		inMyList: false, //别人在我的黑名单中
		inObjList: false, //我在别人的黑名单中
		isApply: false
	},
	hasMsg: false,
	stateList: [{
		name: '加入黑名单',
		id: 1
	}, {
		name: '删除好友',
		color: '#ff3434',
		id: 2
	}]
}
const getters = {
	chatList: state => state.chatList,
	chatItem: state => state.chatItem,
	useState: state => state.useState,
	stateList: state => state.stateList
}
const mutations = {
	setChatList(state, data) {
		state.chatList = data
	},
	setChatItem(state, data) {
		state.chatItem = data
		setToken('chatItem', data)
	},
	setUseState(state, data) {
		if (data && data.key) {
			state.useState[data.key] = data.obj
		} else {
			state.useState = data.obj
		}
	}
}
const actions = {
	//是否够次数
	async toAddPeople({
		commit,
		dispatch,
		state
	}, data) {
		//console.log("data", data);
		//console.log("getToken(isLogin)", getToken('isLogin'));
		if (data.isApply) {
			uni.showToast({
				title: '申请中',
				icon: 'none'
			})
			return;
		}
		if (!data.useNum) {
			return;
		}
		if (!getToken('isLogin')) {
			dispatch('showAuthModal');
			return;
		}
		return new Promise(async (resolve, reject) => {
			//判断当前好友状态
			//扣除对方加好友次数
			let paramsData = {
				'modleName': 'mingpian'
			}
			if (getToken("version") == 1) {
				paramsData['craftsmanId'] = getToken('useNum')
				paramsData['companyId'] = ''
			} else {
				paramsData['companyId'] = getToken('useNum')
				paramsData['craftsmanId'] = ''
			}
			paramsData.setType = data.setType
		//console.log("paramsData", paramsData);
			let result = await updAddFriends(paramsData)
		//console.log("result", result);
			if (result.data && result.data.code == 200) {
				let reco = result.data.data
				if (reco.vipQty > 0 || reco.freeQty > 0) { //有次数
					resolve(true)
				}
			} else {
				uni.showModal({
					title: '提示',
					content: '次数不够，是否立即购买',
					success: (e) => {
						if (e.confirm) {
							uni.navigateTo({
								url: '/pages/meSub/vipPage/vipPage'
							})
						}
					}
				})
			}
		})
	},
	// 加入黑名单
	async usrBlackListInsert({
		commit
	}, userId) {
		if (!getToken('isLogin')) {
			store.dispatch('showAuthModal');
			return
		}
		uni.showModal({
			title: '提示',
			content: '加入黑名单,将不再收到对方的消息',
			success: (e) => {
				if (e.confirm) {
					usrBlackListInsert({
						userId: getToken('useNum'),
						blacklist: 1,
						userType: getToken('version') == 1 ? 0 : 1,
						craftsmanId: userId
					}).then(res => {
						//console.log(res, 888)
						if (res.data && res.data.code == 1) {
							commit('setUseState', {
								key: 'inMyList',
								obj: true
							})
							uni.showToast({
								title: '加入成功',
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '加入失败',
								icon: 'none'
							})
						}
					})
				}
			}
		})
	},
	// 移出黑名单
	async usrBlackListRemove({
		commit
	}, userId) {
		if (!getToken('isLogin')) {
			store.dispatch('showAuthModal');
			return
		}
		uni.showModal({
			title: '提示',
			content: '您确定要将对方移出黑名单吗?',
			success: (e) => {
				if (e.confirm) {
					usrBlackListRemove({
						userId: getToken('useNum'),
						blacklist: 0,
						userType: getToken('version') == 1 ? 0 : 1,
						craftsmanId: userId
					}).then(res => {
						if (res.data && res.data.code == 1) {
							state.inMyList = false
							commit('setUseState', {
								key: 'inMyList',
								obj: false
							})
							uni.showToast({
								title: '移出成功',
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '移出失败',
								icon: 'none'
							})
						}
					})
				}
			}
		})
	},
	//当前好友状态判断
	async friendsStatus({
		commit,
		state
	}, userId) {
		queryBlackList({
			userId: getToken('useNum'),
			craftsmanId: userId
		}).then(res => {
			if (res.data && res.data.code == 1) {
				let item = res.data && res.data.data
				commit('setUseState', {
					key: '',
					obj: {
						isMyFriend: item.isMyFriend, //是我的好友
						isObjFriend: item.isObjFriend, //是对方的好友
						inMyList: item.inMyList, //别人在我的黑名单中
						inObjList: item.inObjList, //我在别人的黑名单中
						isApply: state.useState.isApply
					}
				})
			}
		})
		queryApplyFriend({
			friendsType: 0,
			firendId: userId,
			userId: getToken('useNum')
		}).then(res => {
			if (res.data && res.data.data && res.data.data.id) {
				commit('setUseState', {
					key: 'isApply',
					obj: true
				})
			} else {
				commit('setUseState', {
					key: 'isApply',
					obj: false
				})
			}
		})
	},
	async getUnReadListByEntity({
		commit,
		state
	}) {
		//console.log('请求未读啦！！！')
		if (this.hasMsg) {
			return
		}
		getUnReadListByEntity({
			entity: 'usrChatObjectItem',
			receiveUserNo: getToken('useNum')
		}).then(res => {
			if (res.data && res.data.length) {
				uni.showTabBarRedDot({ //显示红点
					index: 3 //tabbar下标
				})
				state.hasMsg = true
			} else {
				uni.hideTabBarRedDot({ //隐藏红点
					index: 3 //tabbar下标
				})
				state.hasMsg = false
			}
		})
	},
	//当前好友状态判断
	async delFriends({
		commit
	}, userId) {
		uni.showModal({
			title: '提示',
			content: '您确定要删除好友及聊天记录?',
			success: (e) => {
				if (e.confirm) {
					deleteFriend({
						userId: getToken('useNum'),
						friendsType: getToken('version') == 1 ? 0 : 1,
						firendId: userId,
						id: 0
					}).then(res => {
						if (res.data && res.data.code == 1) {
							state.inMyList = false
							commit('setUseState', {
								key: 'isMyFriend',
								obj: false
							})
							uni.showToast({
								title: '删除成功',
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '删除失败',
								icon: 'none'
							})
						}
					})
				}
			}
		})
	}
}
export default {
	state,
	mutations,
	actions,
	getters
}
